package top.chmin.project.cloud.tool;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

/**
 * chmin
 * 2024/1/2 15:07
 * cloud-builder
 */
public class SpringTool {
    private static ApplicationContext applicationContext;

    public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringTool.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        if (applicationContext == null) throw new RuntimeException("applicationContext is null");
        return applicationContext.getBean(clazz);
    }

    public static Object getBeanByName(String name) {
        return applicationContext.getBean(name);
    }
}
